extends Camera2D

var target_zoom = Vector2(1, 1)
var speed = 2 * GLOBAL.GAME_SPEED
var drunk_level = 0.002  # стартовый 0.002
var iter = 0.0


func _ready():
	position = get_parent().get_viewport_rect().size / 2


func _process(delta):
	drunk_level = clamp(drunk_level, 0, 1)

	if zoom != target_zoom:
		zoom = lerp(zoom, target_zoom, speed * delta)

	position = position + Vector2(randf_range(-1, 1), randf_range(-1, 1)) * drunk_level * 20
	position.x = clamp(
		position.x, 1920 / 2.0 - 400 - 2000 * drunk_level, 1920 / 2.0 + 400 + 2000 * drunk_level
	)
	position.y = clamp(
		position.y, 1080 / 2.0 - 200 - 2000 * drunk_level, 1080 / 2.0 + 200 + 2000 * drunk_level
	)

	var prev_wave = 5 * drunk_level * sin(2 * iter + PI / 8)
	iter += delta * speed * 0.5
	if iter >= 2 * PI:
		iter -= 2 * PI
	var zoom_wave = 5 * drunk_level * sin(2 * iter + PI / 8)
	var zoom_delta = prev_wave - zoom_wave + randf_range(-1, 1) * drunk_level * 0.1
	target_zoom += Vector2(zoom_delta, zoom_delta)
	target_zoom.x = clamp(target_zoom.x, 0.7, 2.7)
	target_zoom.y = clamp(target_zoom.y, 0.7, 2.7)

	rotation = (
		PI * drunk_level * sin(iter) * 0.6
		+ PI * drunk_level * sin(2 * iter + PI / 2) * 0.05
		+ PI * drunk_level * sin(3 * iter + PI / 6) * 0.05
		+ PI * drunk_level * sin(5 * iter + PI / 9) * 0.1
	)
